package cn.wolfcode.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.utils.RegexUtils;
import cn.wolfcode.business.utils.VehiclePlateNoUtil;
import cn.wolfcode.common.enums.BusinessStatus;
import cn.wolfcode.common.utils.DateUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.service.IAppointmentService;


/**
 * 养修信息预约Service业务层处理
 *
 * @author 白金之星
 * @date 2023-10-28
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;

    /**
     * 查询养修信息预约
     *
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public Appointment selectAppointmentById(Long id) {
        return appointmentMapper.selectAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     *
     * @param appointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment) {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增养修信息预约
     *
     * @param appointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertAppointment(Appointment appointment) {
        //做非空判断
        Assert.notNull(appointment, "参数不能为空");
        //手机号码校验
        Assert.state(RegexUtils.isPhoneLegal(appointment.getCustomerPhone()), "手机号码格式不正确");
        //车牌号校验
        Assert.notNull(VehiclePlateNoUtil.getVehiclePlateNo(appointment.getLicensePlate()), "车牌号不正确");
        //日期校验
        Assert.isTrue(DateUtils.getNowDate().before(appointment.getAppointmentTime()), "预约时间不合理");


        appointment.setCreateTime(DateUtils.getNowDate());
        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改养修信息预约
     *
     * @param appointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment) {
        //做非空判断
        Assert.notNull(appointment, "参数不能为空");
        //手机号码校验
        Assert.state(RegexUtils.isPhoneLegal(appointment.getCustomerPhone()), "手机号码格式不正确");
        //车牌号校验
        Assert.notNull(VehiclePlateNoUtil.getVehiclePlateNo(appointment.getLicensePlate()), "车牌号不正确");
        //日期校验
        Assert.isTrue(DateUtils.getNowDate().before(appointment.getAppointmentTime()), "预约时间不合理");

        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除养修信息预约
     *
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByIds(Long[] ids) {
        return appointmentMapper.deleteAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentById(Long id) {
        return appointmentMapper.deleteAppointmentById(id);
    }

    /**
     * 到店
     *
     * @param id
     */
    @Override
    public void arralShop(Long id) {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        Assert.state(appointment != null, "参数异常");
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new RuntimeException("状态必须为预约中");
        }
        appointmentMapper.arralShop(id, Appointment.STATUS_ARRIVAL);
    }

    /**
     * 取消预约信息
     *
     * @param id
     */
    @Override
    public void cancel(Long id) {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        Assert.state(appointment != null, "参数异常");
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new RuntimeException("状态必须为预约中");
        }
        appointment.setStatus(Appointment.STATUS_CANCEL);
        appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 生成结算单
     *
     * @param id
     * @return
     */
    @Override
    public Long generate(Long id) {
        if (id == null) {
            throw new RuntimeException("参数异常");
        }
        //判断状态是否为到店状态
        Appointment appointment = appointmentMapper.selectAppointmentById(id);

        //根据预约单查询数据，如果为空，则创建
        Statement statement = statementMapper.queryByAppointmentId(id);
        if (statement == null) {
            statement = new Statement();
            BeanUtils.copyProperties(appointment, statement);
            statement.setId(null);
            statement.setStatus(Statement.STATUS_CONSUME);
            statement.setAppointmentId(id);
            statementMapper.insertStatement(statement);
            //把状态修改成结算单生成状态
            appointmentMapper.updateAuditStatus(id, Appointment.STATUS_SETTLE);
        }
        return statement.getId();
    }
}
